package com.fantastic.service.zjs.impl;

import com.fantastic.common.exception.enums.RespStatusInfo;
import com.fantastic.common.exception.sql.SelectException;
import com.fantastic.common.utils.IsEmptyUtils;
import com.fantastic.mapper.zjs.IntegralMapper;
import com.fantastic.model.pojo.zjs.Integral;
import com.fantastic.service.zjs.IntegralService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author: rxx
 * @since: 2024/6/4 09:09
 * @description: 积分商城信息表service接口实现类
 */
@Service
public class IntegralServiceImpl implements IntegralService {

    @Resource
    private IntegralMapper integralMapper;

    /**
     * 查询积分商城信息
     *
     * @param integral
     * @return
     */
    @Override
    public List<Integral> listIntegral(Integral integral) {
        List<Integral> integrals = integralMapper.listIntegral(integral);
        IsEmptyUtils.listIsEmpty(new SelectException(RespStatusInfo.SELECT_NULL), integrals);
        return integrals;
    }

    /**
     * 查询单个积分商城信息
     *
     * @param integralId
     * @return
     */
    @Override
    public Integral getIntegral(Integer integralId) {
        IsEmptyUtils.integerIsEmpty(new SelectException(RespStatusInfo.SELECT_NULL), integralId);
        return integralMapper.getIntegral(integralId);
    }

    /**
     * 添加积分商城信息
     *
     * @param integral
     * @return
     */
    @Override
    public Integer insertIntegral(Integral integral) {
        IsEmptyUtils.integerIsEmpty(new SelectException(RespStatusInfo.DATA_ERROR),
                integral.getIntegralState());
        //不传出售数量
        if (integral.getIntegralSell() == null) {
            //默认出售数量为0
            integral.setIntegralSell(0);
        }
        return integralMapper.insertIntegral(integral);
    }

    /**
     * 修改积分商城信息
     *
     * @param integral
     * @return
     */
    @Override
    public Integer updateIntegral(Integral integral) {
        IsEmptyUtils.integerIsEmpty(new SelectException(RespStatusInfo.DATA_ERROR), integral.getIntegralId());
        //出售数量修改
        if (integral.getIntegralSell() != null) {
            //获取原来的出售数量
            Integral getIntegral = getIntegral(integral.getIntegralId());
            //将原来的出售数量 + 现在用户兑换的数量
            integral.setIntegralSell(getIntegral.getIntegralSell() + integral.getIntegralSell());
        }
        return integralMapper.updateIntegral(integral);
    }

    /**
     * 删除积分商城信息
     *
     * @param integralId
     * @return
     */
    @Override
    public Integer deleteIntegral(Integer integralId) {
        IsEmptyUtils.integerIsEmpty(new SelectException(RespStatusInfo.SELECT_NULL), integralId);
        return integralMapper.deleteIntegral(integralId);
    }
}
